package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author qzh
 * @create 2020-02-13 2020/2/13
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;



    //1.添加权限信息
    @RequestMapping("/add")
    public Result add(@RequestBody Integer[] checkitemIds, Integer role_id) {
        try {
            roleService.add(checkitemIds, role_id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加权限失败");
        }

        return new Result(true, "添加权限成功");

    }

    //2.查询所有的角色信息
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Role> list = roleService.findAll();
            return new Result(true, "查询所有角色信息成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询所有角色信息失败");
        }
    }

    //4.首先进行用户的分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return roleService.pageQuery(queryPageBean);


    }

    //5.回显角色拥有的权限信息
    @RequestMapping("/findByRoleId")
    public Result findByRoleId(Integer id){
        try {
            List<Integer> list =roleService.findByRoleId(id);
            return new Result(true,"回显勾选的权限信息成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"回显勾选的权限信息失败");
        }
    }

}
